//
//  SC_CompanyDetaiChart_iPad.m
//  PieDrawing
//
//  Created by Yang on 10/18/10.
//  Copyright 2010 __MyCompanyName__. All rights reserved.
//

#import "SC_WRY_ChartView_iPad.h"

#define ChartLeftMargin 40.0f
#define ChartTopMargin 50.0f


static float StandardLevelValue = -10.0f;  // 标准值，默认为负数，即不显示标准线
static float TopLevelHighValue = 0.0f;
static float BottomAmountHighValue = 0.0f;


 
@implementation SC_WRY_ChartView_iPad


- (id)initWithFrame:(CGRect)frame {
    if ((self = [super initWithFrame:frame])) {
        // Initialization code
		self.backgroundColor = [UIColor whiteColor];
		dataArray = [[NSMutableArray alloc] init];
		NormalChartColor = [UIColor colorWithRed:120.0f/255.0f green:164.0f/255.0f blue:213.0f/255.0f alpha:1.0f];
		BackFrameColor = [UIColor colorWithRed:234.0f/255.0f green:234.0f/255.0f blue:234.0f/255.0f alpha:1.0f];
		OverLevelChartColor = [UIColor colorWithRed:217.0f/255.0f green:131.0f/255.0f blue:51.0f/255.0f alpha:1.0f];
    }
    return self;
}
- (void)resetData:(NSArray *)array withStandard:(float )value{
	[dataArray setArray:array];
	if (value > 0) {
		StandardLevelValue = value;
	}
	// calculate the hige values
	for (NSDictionary *dict in array) {
		if([[dict objectForKey:@"level"] floatValue] > TopLevelHighValue) {
			TopLevelHighValue = [[dict objectForKey:@"level"] floatValue];
		}
		if ([[dict objectForKey:@"amount"] floatValue] > BottomAmountHighValue) {
			BottomAmountHighValue = [[dict objectForKey:@"amount"] floatValue];
		}
	}
	if(StandardLevelValue > TopLevelHighValue) {
		TopLevelHighValue = StandardLevelValue;
	}
	TopLevelHighValue += TopLevelHighValue * 0.1f;
	BottomAmountHighValue += BottomAmountHighValue * 0.1f;
	[self setNeedsDisplay];
}

// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect {
	CGContextRef context = UIGraphicsGetCurrentContext();
	[self drawBackgroundFrame:context]; // 画背景
	[self drawStandardLine:context]; // 画标准线
	[self drawTopCurveChart:context]; //  画上半部分
	[self drawBottomBarChart:context];
    // Drawing code
}
- (void)drawBackgroundFrame:(CGContextRef )context {
	CGContextSetStrokeColorWithColor(context,BackFrameColor.CGColor);
	CGContextSetLineWidth(context, 1.0f);
	CGContextSetLineCap(context, kCGLineCapRound);
	CGContextSetLineJoin(context, kCGLineJoinRound);
	
//	// 画框
//	CGPoint addLines[] =
//	{
//		CGPointMake(ChartLeftMargin, ChartTopMargin),
//		CGPointMake(ChartLeftMargin + 640.0f, ChartTopMargin),
//		CGPointMake(ChartLeftMargin + 640.0f, ChartTopMargin + 115.0f),
//		CGPointMake(ChartLeftMargin, ChartTopMargin + 115.0f),
//		CGPointMake(ChartLeftMargin, ChartTopMargin)
//	};
//	CGContextAddLines(context, addLines, sizeof(addLines)/sizeof(addLines[0]));
	CGContextSetAllowsAntialiasing(context, NO);
	for (int j = 0; j < 2; j ++ ) {
		
		// 画横线
		for (int i = 0; i < 4; i ++) {
			CGContextMoveToPoint(context, ChartLeftMargin, ChartTopMargin + 35.0f * i + j * 180.0f);
			CGContextAddLineToPoint(context, ChartLeftMargin + 640.0f, ChartTopMargin + 35.0f * i + j * 180.0f);
		}
		// 画竖线
		for (int i = 0; i < 6; i ++) {
			CGContextMoveToPoint(context, ChartLeftMargin + 128 * i, ChartTopMargin + j * 180.0f);
			CGContextAddLineToPoint(context, ChartLeftMargin + 128 * i, ChartTopMargin + 105.0f + j * 180.0f);
		}
	}
	CGContextMoveToPoint(context, 0.0f, 185.0f);
	CGContextAddLineToPoint(context, 695.0f, 185.0f);
	//画分割线
	
	CGContextStrokePath(context);
	CGContextSetAllowsAntialiasing(context, TRUE);
	
}
- (void)drawTopCurveChart:(CGContextRef )context {
	if ([dataArray count]  < 2) {
		return;
	}
	CGContextSetStrokeColorWithColor(context,NormalChartColor.CGColor);
	CGContextSetLineWidth(context, 2.0f);
	CGContextSetLineCap(context, kCGLineCapRound);
	CGContextSetLineJoin(context, kCGLineJoinRound);
	
	float chartHeight = 105.0f;
	for (int i = 0; i < [dataArray count] - 1; i ++) {
		 int j = i + 1;
		float value1 = [[[dataArray objectAtIndex:i] objectForKey:@"level"] floatValue];
		float value2 = [[[dataArray objectAtIndex:j] objectForKey:@"level"] floatValue];
		float x1 = i * 8 + ChartLeftMargin + 3.0f;
		float y1 = chartHeight -  value1* (chartHeight / TopLevelHighValue) + ChartTopMargin;
		float x2 = j * 8 + ChartLeftMargin + 3.0f;
		float y2 = chartHeight -  value2* (chartHeight / TopLevelHighValue) + ChartTopMargin;
		
//		if (value1 > StandardLevelValue && value2 < StandardLevelValue || value1 < StandardLevelValue && value2 > StandardLevelValue) {
//			
//		}
//		
//		else if (value1 < StandardLevelValue && value2 < StandardLevelValue) {
//			CGContextSetStrokeColorWithColor(context,NormalChartColor.CGColor);
//			
//		}
//		else if (value1 > StandardLevelValue && value2 > StandardLevelValue) {
//			CGContextSetStrokeColorWithColor(context,OverLevelChartColor.CGColor);
//		}
		CGContextMoveToPoint(context, x1, y1);
		CGContextAddLineToPoint(context,x2,y2);
	}
	CGContextStrokePath(context);
}
- (void)drawBottomBarChart:(CGContextRef )context {
	if ([dataArray count]  < 2) {
		return;
	}
	float chartHeight = 105.0f;
	float margin = ChartTopMargin + 180.0f;
	//CGContextSetStrokeColorWithColor(context, boundColor.CGColor);
	CGContextSetFillColorWithColor(context, NormalChartColor.CGColor);
	for (int i = 0; i < [dataArray count]; i ++) {
		float height = chartHeight - [[[dataArray objectAtIndex:i] objectForKey:@"amount"] floatValue] * (chartHeight / BottomAmountHighValue);
		float x = i * 8.0f + ChartLeftMargin;
		float y = margin + chartHeight - height;
		float value = [[[dataArray objectAtIndex:i] objectForKey:@"level"] floatValue];
		if (value < StandardLevelValue) {
			CGContextSetFillColorWithColor(context, NormalChartColor.CGColor);
		}
		else {
			CGContextSetFillColorWithColor(context, OverLevelChartColor.CGColor);
		}

		CGContextAddRect(context, CGRectMake(x
											 , y 
											 , 5.0f
											 , height));
		CGContextDrawPath(context, kCGPathFill);
	}
	//CGContextDrawPath(context, kCGPathFill);
}
- (void)drawStandardLine:(CGContextRef )context {
	if (StandardLevelValue > 0) {
		CGContextSetStrokeColorWithColor(context,OverLevelChartColor.CGColor);
		CGContextSetLineWidth(context, 2.0f);
		CGContextSetLineCap(context, kCGLineCapRound);
		CGContextSetLineJoin(context, kCGLineJoinRound);
		float y = 105.0f - StandardLevelValue * (105.0f / TopLevelHighValue) + ChartTopMargin;
		CGContextMoveToPoint(context, ChartLeftMargin + 1.0f, y);
		CGContextAddLineToPoint(context, 640.0f + ChartLeftMargin - 2.0f, y);
		CGContextStrokePath(context);
	}
}
- (void)dealloc {
    [super dealloc];
}


@end
